package com.redrose.rxandroiddemo.camera;

import android.content.pm.PackageManager;

import androidx.annotation.NonNull;
import androidx.camera.core.Preview;
import androidx.core.app.ActivityCompat;

import com.redrose.rxandroiddemo.R;
import com.redrose.rxandroiddemo.base.BaseActivity;
import com.redrose.rxandroiddemo.databinding.ActivityCameraBinding;

import static android.Manifest.permission.CAMERA;

public class CameraActivity extends BaseActivity<ActivityCameraBinding> {

    private CameraViewModel mCameraViewModel;

    @Override
    protected void initEvent() {

    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_camera;
    }

    @Override
    protected void initView() {
        mCameraViewModel = getActivityViewModel(CameraViewModel.class);
        if (PackageManager.PERMISSION_GRANTED != ActivityCompat.checkSelfPermission(this, CAMERA)) {
            ActivityCompat.requestPermissions(this, new String[]{CAMERA},1000);
            return;
        }
        openCamera();
    }

    @Override
    protected void initData() {

    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        if (requestCode == 1000) {
            for (int i = 0; i < permissions.length; i++) {
                if (grantResults[i] == PackageManager.PERMISSION_GRANTED) {
                    openCamera();
                }
            }
        }
    }

    private void openCamera() {
        MyPreviewView mPreview = mBinding.previewSurfaceview;
        mPreview.startCamera();
    }
}
